package com.rd.student.dashuju.wangbo.day07_31;

import java.util.*;

public class TestExercise {

    public static void main(String[] args) {
        int[] arr = new int[20];
        test(arr);
        test1(arr);
        test2(arr);

        System.out.println("----------");

        int[] arrI = new int[50];
        int n = 0;
        for (int i = 0; i < arrI.length; i++) {
            arrI[i] = n;
            n++;
        }
        test3(arrI);
        test4(arrI);
        test5(arrI);
    }

    public static void test(int[] arr) {
        ArrayList<Integer> arrayList = new ArrayList<>(20);
        while (arrayList.size() < 20) {
            int random = (int) (Math.random() * 50);
            if (!arrayList.contains(random)) {
                arrayList.add(random);
            }

        }
        System.out.println(arrayList);
    }

    public static void test1(int[] arr) {
        Set<Integer> set = new HashSet<>(20);
        while (set.size() < 20) {
            int random = (int) (Math.random() * 50);
            set.add(random);
        }
        System.out.println(set);
    }

    public static void test2(int[] arr) {
        for (int i = 0; i < 20; i++) {
            int random = (int) (Math.random() * 50);

            boolean flag = false;
            for (int j = 0; j < i; j++) {
                if (random == arr[j]) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                i--;
                continue;
            }

            arr[i] = random;
            System.out.print(arr[i] + " ,");
        }
    }

    public static void test3(int[] arrI) {

//        System.out.println(Arrays.toString(arrI));
        ArrayList<Integer> arrayList = new ArrayList<>(arrI.length);
        for (int i = 0; i < 50; i++) {
            arrayList.add(arrI[i]);
        }

        Collections.shuffle(arrayList);

        for (int i = 0; i < 20; i++) {
            System.out.print(arrayList.get(i) + " ,");
        }
    }

    public static void test4(int[] arrI) {
        int[] indexArr1 = new int[50];
        for (int i = 0; i < indexArr1.length; i++) {
            indexArr1[i] = -1;
        }
        System.out.println();
        for (int i = 0; i < 20; i++) {
            int random = (int) (Math.random() * 50);
            if (indexArr1[random] == -1) {
                indexArr1[random] = random;
                System.out.print(arrI[random] + " ,");
            } else {
                i--;
            }
        }
    }
    public static void test5(int[] arrI){
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            int random = (int) (Math.random() * 50);
            arrayList.add(arrI[random]);
            arrI[random] = -1;
        }
        System.out.println();
        System.out.println(arrayList);
    }
}
